#!/bin/bash
perl inicio.pl
simContent=output
exit=0
play=1
pause='5'
continue='8'
next='6'
prev='4'
quit='q'
i=0
CANT="$(awk '/<--/ {flag=1;next} /--\/>/{flag=0; count++}END {print count;} ' $simContent)" 


echo "Simulador de Planificacion de procesos"
sleep 1
clear
while :
do
  read -t 1 -n 1 key

  if [ $key = $quit ] 
  then
    clear
    echo "bye!"
    break
  fi
    
  if [ $key = $pause ] 
  then
    play=0
  fi
  if [ $key = $continue ]
  then
      play=1
  fi
    
  if [ $key = $next ] 
  then
    play=0
    let "i+=1"
  fi
  if [ $key = $prev ] 
  then
    play=0
    if [ $i -gt 1 ]
    then
      let "i = $i-1"
    fi
  fi
  clear
  if [ $play = 1 ] && [ $i -lt $CANT ]
  then
    let "i+=1"
  fi

  awk "/^<--/{flag=1;next}/^--\/>/{flag=0}{if(++m==1)n++;if(n==\"$i\" && flag)print;if(/^--\/>/)m=0}" $simContent
  echo -e "\npause: $pause - play: $continue - next: $next - prev: $prev - quit: $quit"
done